/* $RCSFile: PageStripDetails.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Configuration/Settings/PageStripDetails.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:27  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Threading;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Web;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Settings.Cache;
using AddonNice.Configuration.Settings;

namespace AddonNice.Configuration
{

    /// <summary>
	/// PageStripDetails Class encapsulates the details
	/// -- PageName, PageID and PageOrder -- for a specific Page in the Portal
	/// </summary>
	[XmlType(TypeName="MenuItem")]
	public class PageStripDetails : IComparable 
	{
		/// <summary>
		/// Roles that are allowed to view this page
		/// </summary>
		[XmlAttribute("AuthRoles")]     public string AuthorizedRoles=   string.Empty;	
		/// <summary>
		///  Parent page in the display hierarchy
		/// </summary>
		[XmlAttribute("ParentPageID")]  public int ParentPageID;
		
        /// <summary>
		/// 
		/// </summary>
		[XmlAttribute("PageIndex")]     public int PageIndex;
		
        /// <summary>
		/// Layout name ( and folder name)
		/// </summary>
		[XmlAttribute("PageLayout")]    public string PageLayout    =   string.Empty;	
		
        /// <summary>
		/// Custom Image for menus
		/// </summary>
		[XmlAttribute("PageImage")]     public string PageImage     =   string.Empty;	

        /// <summary>
		///  Page internal name
		/// </summary>
		[XmlAttribute("Label")]         public string PageName      =   string.Empty;

        /// <summary>
        /// Meta Title for page or PageName if not defined, based on current language
        /// </summary>
		[XmlAttribute("PageTitle")]     public string PageTitle     =   string.Empty;
        /// <summary>
        /// Used for displaying page name in menus, PageName if not defined, based on current language
        /// </summary>
		[XmlAttribute("PageKeyPhrase")] public string PageKeyPhrase =   string.Empty;
        
        /// <summary>
		///  Order for a same depth level
		/// </summary>
		[XmlAttribute("PageOrder")]     public int PageOrder;

		/// <summary>
		///  Flags for this page 0: nrml, 1: not_for_menus, 2: Form only, etc. see enum ADNPageFlags
		/// </summary>
		[XmlAttribute("Flags")]         public ADNPageFlags Flags   =   ADNPageFlags.Normal;

		[XmlAttribute("UrlName")]       public string UrlName       =   string.Empty;

		[XmlAttribute("nbModules")]     public int nbModules        =   0;
        

		/// <summary>
		/// Private Internal id for this page, key stored in db
		/// </summary>
		int _pageID;

		/// <summary>
		/// Public Internal ID
		/// </summary>
		[XmlAttribute("ID")]
		public int PageID
		{
			get
			{
				return _pageID;
			}
			set
			{
				_pageID = value;
			}       
		}

		/// <summary>
		/// Private Internal id for this page's portal, from 1/12/08 pages displayed could come either from current PortalID from portalSettings, either from commonPages Portal with id-2
		/// </summary>
		int _portalID;

		/// <summary>
		/// Public Internal ID
		/// </summary>
		[XmlAttribute("PortalID")]
		public int PortalID
		{
			get
			{
				return _portalID;
			}
			set
			{
				_portalID = value;
			}       
		}

        /// <summary>
        /// Is this a from from commonPages portal
        /// </summary>
        public bool isCommonPage
		{
			get
			{
				return _portalID == StStr.CommonPortalID;
			}
		}

        /// <summary>
        /// Is this a page from same portal as current portal
        /// </summary>
        public bool? isOwnedPage
		{
			get
			{
                if ( HttpContext.Current == null)
                    return null;
                PortalSettings portalSettings = (PortalSettings)HttpContext.Current.Items[StStr.strPortalSettings];
                if ( portalSettings == null)
                    return null;
				return _portalID == portalSettings.PortalID;
			}
		}


///////////////////////// Compatibility with PageItem		
        /// <summary>
        /// Order
        /// </summary>
        public int Order 
        {
            get 
            {
                return PageOrder;
            }
            set 
            {
                PageOrder = value;
            }
        }    

        /// <summary>
        /// Name
        /// </summary>
        public string Name 
        {
            get 
            {
                return PageName;
            }
            set 
            {
                PageName = value;
            }
        }

        /// <summary>
        /// ID
        /// </summary>
        public int ID
        {
            get 
            {
                return _pageID;
            }
            set 
            {
                _pageID = value;
            }
        }  

        /// <summary>
        /// ParentTabID
        /// </summary>
        public int ParentTabID 
        {
            get 
            {
                return ParentPageID;
            }
            set 
            {
                ParentPageID = value;
            }
        }    

        /// <summary>
		/// List of contained pages
		/// </summary>
		[XmlArray(ElementName="MenuGroup", IsNullable = false)]
        public PageStripDetailsList Pages 
		{
			get
			{
                int searchPortal        =   PortalID;
                if (PortalID == StStr.CommonPortalID)
                {
                    PortalSettings ps   = (PortalSettings)HttpContext.Current.Items[StStr.strPortalSettings];
                    if ( ps != null)
                        searchPortal    =   ps.PortalID;
                }
				string cacheKey         =   Key.TabNavigationSettings (searchPortal,_pageID);
                PageStripDetailsList tabs;
                bool ExistInCache       =   CurrentCache.Exists (cacheKey);
                if ( NavTraceSwitch.Sw.TraceVerbose )
				    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageStripDetail Pages GET found in cache: {0}, PortalID: {1}, PageID: {2}",
                        ExistInCache, searchPortal,_pageID),NavTraceSwitch.Sw.Info);
				if (!ExistInCache)
				{
					tabs                =   PageSettings.GetPageSettingsPagesBox(searchPortal,_pageID);
					CurrentCache.Insert(cacheKey, tabs,CurrentCache.GlobalPagesDependency());
				}
				else
				{
                    tabs                =   (PageStripDetailsList)CurrentCache.Get(cacheKey);
				}
				return tabs;
			}
		}

        /// <summary>
        /// Public comparer
        /// </summary>
        public int CompareTo(object value) 
        {
            if (value == null) 
                return 1;

            int compareOrder = ((PageStripDetails)value).PageOrder;
            
            if (this.PageOrder == compareOrder) return 0;
            if (this.PageOrder < compareOrder) return -1;
            if (this.PageOrder > compareOrder) return 1;
            return 0;
        }

        public bool OkForMenus
        {
            get { return ( (Flags & ADNPageFlags.NotOkForMenu) != ADNPageFlags.NotOkForMenu ); }
        }

        public bool FormsOnly
        {
            get { return ( (Flags & ADNPageFlags.OnlyFormsAuthentication) == ADNPageFlags.OnlyFormsAuthentication ); }
        }

        public bool AuthenticatedOnly
        {
            get { return ( (Flags & ADNPageFlags.OnlyAuthenticated) == ADNPageFlags.OnlyAuthenticated ); }
        }

        public bool LoginPage
        {
            get { return ( (Flags & ADNPageFlags.IsLoginPage) == ADNPageFlags.IsLoginPage ); }
        }

        public bool RegisterPage
        {
            get { return ( (Flags & ADNPageFlags.IsRegisterPage) == ADNPageFlags.IsRegisterPage); }
        }
	}
}
